<?php
/**
 * Created by PhpStorm.
 * User: zl
 * Date: 2017/6/15
 * Time: 14:17
 */
require_once ('Common.php');
class Files{
    function createFilePath($type, $id){
        //文件存放URL
        $fileURL = '';
        //根据类型构造不同的文件夹
        switch($type){
            //如果是审批附件
            case 1:{
                $fileURL = 'uploads/files/approval/'.$id.'/';
                break;
            }
            //如果是报销附件
            case 2:
                $fileURL = 'uploads/files/fee/'.$id.'/';
                break;
        }
        return $fileURL;
    }

    function createDirectory($fileURL){
        if(is_dir($fileURL)){
            return;
        }
        $res = mkdir(iconv("UTF-8", "GBK", $fileURL), 0777, true);
        if(!$res){
            return false;
        }
        else {
            return true;
        }

    }

    function delFile($dir){
        if(!is_dir($dir)){
            return;
        }
        if($handle = opendir($dir)){
            while(false != ($item = readdir($handle))){
                if($item != "." && $item != ".."){
                    unlink("$dir/$item");
                }
            }
            closedir($handle);
        }
    }

    function delFileAndDirectores($path){
            //给定的目录不是一个文件夹
            if(!is_dir($path)){
                return null;
            }

            $fh = opendir($path);
            while(($row = readdir($fh)) !== false){
                //过滤掉虚拟目录
                if($row == '.' || $row == '..'){
                    continue;
                }

                if(!is_dir($path.'/'.$row)){
                    unlink($path.'/'.$row);
                }
                delFileAndDirectores($path.'/'.$row);

            }
            //关闭目录句柄，否则出Permission denied
            closedir($fh);
            //删除文件之后再删除自身
            if(!rmdir($path)){
                echo $path.'无权限删除<br>';
            }
            return true;
    }

    function uploadFile($files, $fileURL){
        //计算到底上传了多少文件
        $count = count($files['files']['name']);

        $all_files = '';

        //上传文件过程，处理方式为只要一个文件上传失败，就退出，返回错误
        for ($i = 0; $i < $count; $i++) {
            $tmpfile = $files['files']['tmp_name'][$i];
            $dstfile = $fileURL.$files['files']['name'][$i];
            $dstfile = iconv('UTF-8','GB2312', $dstfile);
            if (!move_uploaded_file($tmpfile, $dstfile)) {
                return false;
            }
            else{
                $all_files .= $_FILES['files']['name'][$i].";";
            }
        }
        return $all_files;
    }

    //辅助函数，处理上传文件改变状态
    function uploadFileState($id, $all_files, $type){
        $conn = new SQLBase();
        switch($type){
            case 1:
                $conn = new SQLBase();
                $query = "UPDATE travel_approval SET with_file='".$all_files."' WHERE expense_claim_ID='".$id."'";
                $result = $conn->queryData($query);
                break;
            case 2:
                $query = "UPDATE travel_fee_approve SET self_drive_img = '$all_files' WHERE travel_fee_id = '$id'";
                $result = $conn->queryData($query);
                break;
        }

        if($result){
            return true;
        }else{
           return false;
        }
    }


}